package com.lei.baweiliao.view.fragment;


import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.lei.baweiliao.R;
import com.lei.baweiliao.threadhelper.ThreadHelper;
import com.lei.baweiliao.utils.GreenDaoUtils;
import com.lei.baweiliao.view.activity.LoginActivity;

/**
 * A simple {@link Fragment} subclass.
 */
public class MeFragment extends Fragment {
    private Button meTuiBt;

    public MeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_me, container, false);

        meTuiBt = (Button) view.findViewById(R.id.me_tui_bt);

        meTuiBt.setText("退出登录（"+EMClient.getInstance().getCurrentUser()+"）");

        meTuiBt.setOnClickListener(V -> {
            ThreadHelper.getInstance().getExecutors().submit(() ->{
                EMClient.getInstance().logout(true, new EMCallBack() {
                    @Override
                    public void onSuccess() {
                        // TODO Auto-generated method stub
                        getActivity().runOnUiThread(() ->{
                            Toast.makeText(getContext(), "退出成功", Toast.LENGTH_SHORT).show();
                            SPUtils.getInstance().put("login",false);
                            startActivity(new Intent(getActivity(), LoginActivity.class));
                            getActivity().finish();
                        });
                    }

                    @Override
                    public void onProgress(int progress, String status) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onError(int code, String message) {
                        // TODO 退出失败
                        Toast.makeText(getContext(), "退出失败" + message, Toast.LENGTH_SHORT).show();
                    }
                });
            });
        });
        return view;
    }

}
